package com.zjdiepu.www.biz.system;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.commpent.RedisObjectClient;
import com.zjdiepu.www.dao.system.BaseSysParamsMapper;
import com.zjdiepu.www.ibiz.system.IBaseSysParamsService;
import com.zjdiepu.www.model.system.dto.BaseSysParams;
import com.zjdiepu.www.model.system.ext.BaseSysParamsExt;

/**
 * 系统参数Service层操作实现<br/>
 * @author zhangpp<br/>
 * @time 2017年05月18日 10时15分<br/>
 * @since 1.0<br/>
 */
@Service
public class BaseSysParamsServiceImpl extends BaseService implements IBaseSysParamsService {
	public static final Logger logger = LoggerFactory.getLogger(BaseSysParamsServiceImpl.class);

	@Autowired
	private BaseSysParamsMapper baseSysParamsMapper;
	@Resource
	private RedisObjectClient redisObjectClient;
	
	@Override
	public String getValueByCodeWithPool(RedisConnection redisConnection, String code) {
		if(StringUtils.isBlank(code)) {
			return null;
		}
		BaseSysParams baseSysParam = redisObjectClient.hGet(redisConnection, BaseSysParams.class.getSimpleName(), code);
		if(baseSysParam == null) {
			return null;
		}
		return baseSysParam.getValueParam();
	}
	
	@Override
	public String getValueByCode(String code) {
		if(StringUtils.isBlank(code)) {
			return null;
		}
		BaseSysParams baseSysParam = redisObjectClient.hGet(BaseSysParams.class.getSimpleName(), code);
		if(baseSysParam == null) {
			return null;
		}
		return baseSysParam.getValueParam();
	}
	
	
	
	@Override
	public void saveUpdateAndDelete(BaseSysParams baseSysParams, OperateType operType) {
		try {
			switch (operType) {
			case add:
				baseSysParams.initInsertData(baseSysParams.getModifier(), baseSysParams.getModifierName());
				baseSysParamsMapper.insert(baseSysParams);
				break;
			case edit:
				baseSysParams.initUpdateData(baseSysParams.getModifier(), baseSysParams.getModifierName());
				// 影响的记录数
				int updateCount = baseSysParamsMapper.updateByPrimaryKeySelective(baseSysParams);
				if(updateCount == 0){
					throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
				} else if(updateCount > 1){
					throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
				}
				break;
			case delete:
				baseSysParams.initDeleteData(baseSysParams.getModifier(), baseSysParams.getModifierName());
				// 影响的记录数
				int deleteCount = baseSysParamsMapper.updateByPrimaryKeySelective(baseSysParams);
				if(deleteCount == 0){
					throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
				} else if(deleteCount > 1){
					throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
				}
				break;
			default:
				break;
			}
		} finally {
			String codeParam = baseSysParams.getCodeParam();
			BaseSysParamsExt baseSysParamsExt = new BaseSysParamsExt();
			baseSysParamsExt.setBaseWhere();
			baseSysParamsExt.setRows(1);
			baseSysParamsExt.setCodeParam(codeParam);
			List<BaseSysParams> dbBaseSysParams = baseSysParamsMapper.selectByExt(baseSysParamsExt);
			redisObjectClient.execute(new RedisCallback<Boolean>() {
				@Override
				public Boolean doInRedis(RedisConnection redisConnection) throws DataAccessException {
					if(CollectionUtils.isNotEmpty(dbBaseSysParams)){
						BaseSysParams dbBaseSysParam = dbBaseSysParams.get(0);
						redisObjectClient.hSet(redisConnection, BaseSysParams.class.getSimpleName(), codeParam, dbBaseSysParam);
					} else {
						redisObjectClient.hDel(redisConnection, BaseSysParams.class.getSimpleName(), codeParam);
					}
					return true;
				}
			});
		}
	}
	
	@Override
	public List<BaseSysParams> selectByExt(BaseSysParamsExt baseSysParamsExt) {
		return baseSysParamsMapper.selectByExt(baseSysParamsExt);
	}
	
	@Override
	public int countByExt(BaseSysParamsExt baseSysParamsExt){
		return baseSysParamsMapper.countByExt(baseSysParamsExt);
	}
	
	@Override
	public BaseSysParams selectById(String id) {
		return baseSysParamsMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			baseSysParamsMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(BaseSysParams baseSysParams) {
		return baseSysParamsMapper.countRefined(baseSysParams);
	}

	@Override
	public BaseSysParams selectRefined(BaseSysParams baseSysParams) {
		return baseSysParamsMapper.selectRefined(baseSysParams);
	}
	
	@Override
	public int deletedRefined(BaseSysParams baseSysParams) {
		return baseSysParamsMapper.deletedRefined(baseSysParams);
	}

	@Override
	public boolean repeat(BaseSysParams baseSysParams) {
		int count = countRefined(baseSysParams);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(baseSysParams.getId())) {
			return true;
		}
		BaseSysParams dbItem = selectRefined(baseSysParams);
		if(null == dbItem) {
			return false;
		}
		return !baseSysParams.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return baseSysParamsMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<BaseSysParams> baseSysParamss) {
		int count = baseSysParamsMapper.insertBatch(baseSysParamss);
		cacheParams();
		return count;
		
	}
	
	private List<BaseSysParams> cacheParams(){
		BaseSysParamsExt baseSysParamsExt = new BaseSysParamsExt();
		baseSysParamsExt.withOutPage();
		baseSysParamsExt.setBaseWhere();
		List<BaseSysParams> baseSysParams = baseSysParamsMapper.selectByExt(baseSysParamsExt);
		redisObjectClient.execute(new RedisCallback<Boolean>() {
			@Override
			public Boolean doInRedis(RedisConnection redisConnection) throws DataAccessException {
				for(BaseSysParams baseSysParam : baseSysParams) {
					redisObjectClient.hSet(redisConnection, BaseSysParams.class.getSimpleName(), baseSysParam.getCodeParam(), baseSysParam);
				}
				return true;
			}
		});
		return baseSysParams;
	}
}
